package org.example.day20240827.Demo;

import java.util.HashMap;
import java.util.Map;

public class WordCount {
    public static void main(String[] args) {
        String sentence = "he is a boy this is a book that is a book";
        String[] arr = sentence.split(" ");
        Map<String,Integer> wordMap = new HashMap<>();
        for (int i=0; i<arr.length; i++){
            if(wordMap.containsKey(arr[i])){
                int count = wordMap.get(arr[i]);
                count++;
                wordMap.put(arr[i],count);
            }else {
                wordMap.put(arr[i],1);
            }
        }
        for (Map.Entry<String,Integer> entry : wordMap.entrySet()){
            System.out.println(entry.getKey()+"-----"+entry.getValue());
        }
    }
}
